import QtQuick 1.0

XmlListModel {
    id: demotyModel

    // numer strony z dzialu, dla radnom zawsze 0, domyslnie 1
    property int pgNumber: 1

    // nazwa dzialu do pobrania
    property string dzial: "main"

    source: getSource(dzial,pgNumber)
    query: "/demotywatory/demotywator"


    XmlRole {name: "demoty_id";  query: "@id/string()"}
    XmlRole {name: "demoty_exist"; query: "@exist/string()" }


    XmlRole {name: "demoty_url"; query: "url/string()" }
    XmlRole {name: "demoty_title"; query: "title/string()"}
    XmlRole {name: "demoty_picture_thumb"; query: "picture_thumb/string()" }
    XmlRole {name: "demoty_text"; query: "text/string()" }
    XmlRole {name: "demoty_demot_status"; query: "status/string()" }
    XmlRole {name: "demoty_dateadded"; query: "dateadded/string()" }
    XmlRole {name: "demoty_author"; query: "author/string()"}
    XmlRole {name: "demoty_picture_big"; query: "picture_big/string()" }
    XmlRole {name: "demoty_picture"; query: "picture/string()"}
    XmlRole {name: "demoty_original"; query: "original/string()"}
    XmlRole {name: "demoty_points"; query: "points/number()" }
    XmlRole {name: "demoty_votes"; query: "votes/number()"}
    XmlRole {name: "demoty_height_500"; query: "height_500/number()"}
    XmlRole {name: "demoty_comments"; query: "comments/number()" }

    function getSource(xdzial,xnumer){
        //  To jest dobre miejsce żeby dodac swoj klucz dostepu do API demotow
        // const api="DemotyAPIKey123"
        const url="http://demotywatory.pl/api/get/";
        var numer=getPgNum(xdzial,xnumer);
        return url+xdzial+"/page/"+numer+"/"+api;
    }

    function getPg(nrStrony) {
        getSource(dzial,nrStrony)
        reload();
    }
    function getPgNum(xdzial,nrstrony) {
        if (xdzial==="random") return 0;
        if (nrstrony<1) return 1;

        return nrstrony;
    }
}


